package cn.ht.jywrwater.module.ht.controller.admin.watersurvey;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;

import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;

import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ht.jywrwater.module.ht.controller.admin.watersurvey.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.watersurvey.WaterSurveyDO;
import cn.ht.jywrwater.module.ht.convert.watersurvey.WaterSurveyConvert;
import cn.ht.jywrwater.module.ht.service.watersurvey.WaterSurveyService;

@Tag(name = "管理后台 - 运行监测概览")
@RestController
@RequestMapping("/ht/water-survey")
@Validated
public class WaterSurveyController {

    @Resource
    private WaterSurveyService waterSurveyService;

    @PostMapping("/create")
    @Operation(summary = "创建运行监测概览")
    @PreAuthorize("@ss.hasPermission('ht:water-survey:create')")
    public CommonResult<Long> createWaterSurvey(@Valid @RequestBody WaterSurveyCreateReqVO createReqVO) {
        return success(waterSurveyService.createWaterSurvey(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新运行监测概览")
    @PreAuthorize("@ss.hasPermission('ht:water-survey:update')")
    public CommonResult<Boolean> updateWaterSurvey(@Valid @RequestBody WaterSurveyUpdateReqVO updateReqVO) {
        waterSurveyService.updateWaterSurvey(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除运行监测概览")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:water-survey:delete')")
    public CommonResult<Boolean> deleteWaterSurvey(@RequestParam("id") Long id) {
        waterSurveyService.deleteWaterSurvey(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得运行监测概览")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:water-survey:query')")
    public CommonResult<WaterSurveyRespVO> getWaterSurvey(@RequestParam("id") Long id) {
        WaterSurveyDO waterSurvey = waterSurveyService.getWaterSurvey(id);
        return success(WaterSurveyConvert.INSTANCE.convert(waterSurvey));
    }

    @GetMapping("/list")
    @Operation(summary = "获得运行监测概览列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-survey:query')")
    public CommonResult<List<WaterSurveyRespVO>> getWaterSurveyList(@RequestParam("ids") Collection<Long> ids) {
        List<WaterSurveyDO> list = waterSurveyService.getWaterSurveyList(ids);
        return success(WaterSurveyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得运行监测概览分页")
    @PreAuthorize("@ss.hasPermission('ht:water-survey:query')")
    public CommonResult<PageResult<WaterSurveyRespVO>> getWaterSurveyPage(@Valid WaterSurveyPageReqVO pageVO) {
        PageResult<WaterSurveyDO> pageResult = waterSurveyService.getWaterSurveyPage(pageVO);
        return success(WaterSurveyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出运行监测概览 Excel")
    @PreAuthorize("@ss.hasPermission('ht:water-survey:export')")
    @OperateLog(type = EXPORT)
    public void exportWaterSurveyExcel(@Valid WaterSurveyExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WaterSurveyDO> list = waterSurveyService.getWaterSurveyList(exportReqVO);
        // 导出 Excel
        List<WaterSurveyExcelVO> datas = WaterSurveyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "运行监测概览.xls", "数据", WaterSurveyExcelVO.class, datas);
    }

    @GetMapping("/allByType")
    @Operation(summary = "获得所有类型监测状态")
//    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-survey:query')")
    public CommonResult<List<WaterSurveyTypeListVO>> getAllByType() {
        List<WaterSurveyTypeListVO> list = waterSurveyService.getAllByType();
        return success(list);
    }

    @GetMapping("/typePosition")
    @Operation(summary = "获得类型所有类设备位置")
    @Parameter(name = "types", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-survey:query')")
    public CommonResult<List<WaterSurveyTypeListVO>> typePosition(@RequestParam("types") Collection<Integer> types) {
        List<WaterSurveyTypeListVO> list = waterSurveyService.getByType(types);
        return success(list);
    }

    @GetMapping("/typeStatusCounts")
    @Operation(summary = "获得类型设备是否在线数量")
    @Parameter(name = "type", description = "类型", required = true, example = "1024")
    @Parameter(name = "status", description = "状态", required = true, example = "0")
    @PreAuthorize("@ss.hasPermission('ht:water-survey:query')")
    public CommonResult<WaterSurveyCountVO> typeStatuscounts(@RequestParam("type") Integer type, @RequestParam("status") Integer status) {
        WaterSurveyCountVO result = waterSurveyService.countByTypeAndStatus(type,status);
        return success(result);
    }

}
